﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;

namespace Simple.Services;

public class QHSEMenuService
{
    private readonly SimpleDbContext _context;
    public QHSEMenuService(SimpleDbContext context)
    {
        _context = context;
    }
    public async Task<List<QHSEMenuModel>> GetAsync()
    {
        var menus = await _context.Set<QHSESysMenu>().ToListAsync();
        return MapperHelper.Map<List<QHSEMenuModel>>(menus);
    }
    public async Task<PageResultModel<QHSEMenuModel>> GetPageAsync(MenuPageInputModel input)
    {
        var result = new PageResultModel<QHSEMenuModel>();
        var query = _context.Set<QHSESysMenu>().AsNoTracking().AsQueryable();

        if (!string.IsNullOrEmpty(input.Name))
        {
            query = query.Where(u => EF.Functions.Like(u.Name, $"%{input.Name}%"));

        }


        // 获取总数量
        result.TotalRows = await query.CountAsync();

        // 分页查询
        query = query.OrderBy(u => u.Name).Page(input.PageNo, input.PageSize);
        var students = await query.ToListAsync();
        result.Rows = MapperHelper.Map<List<QHSEMenuModel>>(students);

        result.SetPage(input);
        result.CountTotalPage();

        return result;
    }

    public async Task<int> AddAsync(QHSEMenuModel model)
    {
        var menu = MapperHelper.Map<QHSESysMenu>(model);
        await _context.AddAsync(menu);
        return await _context.SaveChangesAsync();
    }
}
